<?php

namespace app\common\model\wechat;

use app\common\model\user\User;
use tmcore\base\BaseModel;
use tmcore\traits\ModelTrait;

/**
 * 微信用户模型
 */
class WechatUser extends BaseModel
{
    use ModelTrait;

    /**
     * 用户获取器
     * @param $value
     * @param $data
     * @return string|array
     */
    public function getUidDescAttr($value, $data)
    {
        $arr = explode(',', $data['uid']);
        $result = [];

        foreach ($arr as $key => $value) {
            if($value) {
                $row = User::where(['id' => $value])->field('id, account, avatar')->findOrEmpty();
                if(!$row->isEmpty()) array_push($result, ['id' => $row['id'], 'name' => $row['account'], 'avatar' => $row['avatar']]);
            }
        }

        return $result;
    }

    /**
     * 标签描述获取器
     * @param $value
     * @param $data
     * @return string|string[]
     */
    public function getTagListDescAttr($value, $data)
    {
        if($data['tagid_list']) {
            $tag_list = explode(',', $data['tagid_list']);
            $arr = [];
            foreach ($tag_list as $k => $v) {
                $arr[$k] = WechatTag::where(['tag_id' => $v])->value('name');
            }
            return implode(',', $arr);
        } else {
            return '';
        }
    }

    /**
     * 是否关注获取器
     * @param $value
     * @param $data
     * @return string|array
     */
    public function getIsSubscribeDescAttr($value, $data)
    {
        $attrs = [
            1 => '是',
            0 => '否',
        ];

        return $attrs[$data['is_subscribe']];
    }

    /**
     * 性别获取器
     * @param $value
     * @param $data
     * @return string|array
     */
    public function getSexDescAttr($value, $data)
    {
        $attrs = [
            1 => '男',
            2 => '女',
            0 => '未知',
        ];

        return $attrs[$data['sex']];
    }
}